<?php
//员工管理
namespace App\Http\Controllers\App;
use App\Http\Controllers\App\BaseController as Controller;

use App\Models\ShopClerk;
use Illuminate\Http\Request;
use App\Models\Shop;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ManagementEmployees extends Controller
{
    protected $shopClerk;
    protected $thisLoginClerk;
//
    public function __construct(ShopClerk $shopClerk,Request $request)
    {
        $this->shopClerk = $shopClerk;
        $checkThisClerk = $this->getShopUserID($request->token,$request->device_id);
        if(!$checkThisClerk){
            return show(2001,'无效token');
        }else{//判断用户角色
            //权限处理
            switch($checkThisClerk->role){
                case -1:    // 商家
                case 2:     // 经理
                    $this->thisLoginClerk = $checkThisClerk;
                    break;
                case 0:     //财务
                case 1:     // 服务员
                case 3:     // 主管
                    return show(2002,'没有权限');
                    break;
                default:
                    return show(2002,'没有权限');
                    break;
            }
        }
    }

    //所有员工信息
    public function getAllClerks(Request $request)
    {
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'shop_id.*'=>'请选择对应的商家',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }

        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }


        $res = $this->shopClerk::where('shop_id',$request->shop_id)->where('status',1)->get();
        return show(200,'员工信息',$res);
    }


    public function addClerk(Request $request){
        $rules = [
            'name' => 'required',
            'password' => 'required',
            'mobile'=>'required|mobile',//|unique:shop_clerks,mobile',
            'role'=>'required|integer|in:0,1,2,3',                          //      (0财务，1服务员，2经理，3主管)
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'name.*' => '请填写名称',
            'password.*' => '请填写密码',
            'mobile.required' => '请填写手机号',
            'mobile.mobile' => '手机号格式有误',
//            'mobile.unique' => '手机号已经存在',
            'role.*' =>  '角色有误',
            'shop_id.*' => '商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }
            }

            $count = $this->shopClerk::where('shop_id',$request->shop_id)->where('mobile',$request->mobile)->count();
            if ($count >= 1){
                return show(2003,'手机号重复，不允许添加');
            }
            $password = bcrypt($request->password);//md5(\request('password').''.'zlt');
            $employeeId = ''.$request->shop_id.''.$request->role;       // 员工编号
            for ($i = 1; $i <= 8; $i++) {
                $employeeId.= chr(rand(97, 122));
            }

            $data = array_merge(
                \request(['name','mobile','role','shop_id']),
                compact('password','employeeId')
            );
            $this->shopClerk::create($data);
            return show(200,'添加员工成功');
        }catch (\Exception $e){
            return show(201,'添加员工失败');
        }

    }

    // 展示员工信息
    public function showOneClerk(Request $request){

        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'id.*'=>'员工编号有误',
            'shop_id.*'=>'商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }

        $res = $this->shopClerk::where('shop_id',$request->shop_id)->find($request->id);
        return show(200,'员工信息',$res);
    }
    // 编辑员工信息
    public function editClerk(Request $request){

        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'name' => 'string',
            'password' => 'string',
//            'mobile'=>'mobile',
            'role'=>'required|integer|in:0,1,2,3',
            'employeeId' =>'string',
            'avatar'=>'',
            'sex'=>'in:1,0'
        ];
        $messages = [
            'id.*'=>'该员工不存在',
            'name.*'=>'该员工不存在',
            'password.*'=>'请填写正确的密码',
//            'mobile.*'=>'请填写正确的手机号',
            'role.*'=>'该员工角色有误',
            'employeeId.*'=>'该员工编号不存在',
            'avatar.*'=>'请上传头像',
            'sex.*'=>'请认真填写性别'
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }

        $clerk = $this->shopClerk::find($request->id);
        if (!$clerk){
            return show(2003,'请选择正确的员工');
        }
        $now_role = $this->thisLoginClerk->role;

        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($clerk->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }
        try{
            $password = $request->password;
            $avatar = $request->avatar;

            if (isset($password)){
                $password=bcrypt($password);
                if (isset($avatar)){
                    $data = array_merge(
                        \request(['name','role','avatar','employeeId','sex']),
                        compact('password')
                    );
                }else{
                    $data = array_merge(
                        \request(['name','role','employeeId','sex']),
                        compact('password')
                    );
                }

            }else{
                if (isset($avatar)){
                    $data = \request(['name','role','avatar','employeeId','sex']);
                }else{
                    $data = \request(['name','role','employeeId','sex']);
                }
            }

            $clerk->update($data);
        }catch (\Exception $e){
            dd($e->getMessage());
            return show(201,'员工信息修改失败');
        }
        return show(200,'员工信息修改成功');
    }

    public function delClerk(Request $request){
        $rules = [
            'id'=>'required|integer|exists:shop_clerks,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=>'请选择正确的员工',
            'shop_id.*'=>'请选择正确的商家'
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
//        return $this->thisLoginClerk;
        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
            if( $request->id == $this->thisLoginClerk->id){
                return show(2002,'没有权限');
            }
        }

        $res = $this->shopClerk::where('shop_id',$request->shop_id)->where('id',$request->id)->delete();
        if(!$res){
            return show(201,'员工信息删除失败');
        }
        return show(200,'员工信息删除成功');
    }

}
